<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
	class custom_datetime
	{
		function custom_datetime(){
			date_default_timezone_set('Asia/Bangkok');
		}
		
		function month_thai(){
			return array('01' => 'มกราคม',
							'02' => 'กุมภาพันธ์',
							'03' => 'มีนาคม',
							'04' => 'เมษายน',
							'05' => 'พฤษภาคม',
							'06' => 'มิถุนายน',
							'07' => 'กรกฎาคม',
							'08' => 'สิงหาคม',
							'09' => 'กันยายน',
							'10' => 'ตุลาคม',
							'11' => 'พฤศจิกายน',
							'12' => 'ธันวาคม');
		}
		
		// format
		function default_date_format($date){
			return date('Y-m-d', strtotime($date));
		}
		function default_time_format($time){
			return date('H:i:s', $time);
		}
		
		function current_datetime(){
			return date('Y-m-d H:i:s');
		}
		
		// Date
		public function current_date($format=null){				
			if (is_null($format))			
				return $this->default_date_format($this->current_year() . '-' . date('m-d'));
			else 
				return date($format);
		}
		public function current_date_th(){				
			return $this->current_year_th() . '-' . date('m-d');								
		}
		
		// Time
		public function current_time($no_sec=false){		
			if (!$no_sec)
				return $this->default_time_format(time());
			else 
				return date("H:i", time());
		}	
		
		// Year
		public function current_year(){			
			return date('Y');
		}
		public function current_year_th(){			
			return date('Y')+543;
		}
		public function current_short_year(){		
			return date('y');		
		}
		public function current_short_year_th(){		
			return date('y')+43;
		}
		
		// Miscellaneous
		public function BEyear_to_CEyear($date){				
			$oDate = new DateTime($date);			
			$oYear = $oDate->format('Y');
						
			if ($oYear > 2500)				
				return $this->default_date_format($oDate->format('d-m') . '-' . ($oYear - 543));
			else			
				return $this->default_date_format($oDate->format('d-m') . '-' . $oYear);			
		}
		public function CEyear_to_BEyear($date)
		{
			$oDate = new DateTime($date);			
			$oYear = $oDate->format('Y');
						
			if ($oYear < 2500)				
				return ($oYear + 543) . '-' . $oDate->format('d-m');
			else			
				return ($oYear) . '-' . $oDate->format('d-m');
		}

		/*
		 * compare time between two object 
		 * if return 1 date1 more than date2
		 * if return 0 date1 equal date2
		 * if return -1 date1 less than date2
		 * @param standing date
		 * @param operate date
		 * @return int
		*/ 
		function compare_date($date1, $date2=null){			
			if (is_null($date2))			
				$date2 = date('Y-m-d');
			
			$oDate1 = strtotime($this->BEyear_to_CEyear($date1));			
			$oDate2 = strtotime($this->BEyear_to_CEyear($date2));			
					
			if ($oDate1 > $oDate2)
				return 1;
			elseif ($oDate1 === $oDate2)
				return 0;
			else
				return -1;
		}	
		
		function in_range($start_date, $end_date, $check_date){
		  // Convert to timestamp
		  $start_ts = strtotime($start_date);
		  $end_ts = strtotime($end_date);
		  $user_ts = strtotime($check_date);
		
		  // Check that user date is between start & end
		  return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));
		}
		
		function date_diff($str_date1,$str_date2){
			return (strtotime($str_date2) - strtotime($str_date1))/  ( 60 * 60 * 24 );  // 1 day = 60*60*24
		}
		function time_diff($str_time1,$str_time2){
			return (strtotime($str_time2) - strtotime($str_time1))/  ( 60 * 60 ); // 1 Hour =  60*60
		}
		function datetime_diff($str_datetime1,$str_datetime2){
			return (strtotime($str_datetime2) - strtotime($str_datetime1))/  ( 60 * 60 ); // 1 Hour =  60*60
		}
	}
?>